package com.dc.weixin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dc.base.BaseApiService;
import com.dc.base.BaseResponse;
import com.dc.constant.Constants;
import com.dc.core.utils.RedisUtils;
import com.dc.weixin.service.IVerifyCodeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: KEYD
 * @Date: 2019/12/27 13:35
 * @Desc: 微信验证的实现类
 */
@RestController
public class VerifyCodeServiceImpl extends BaseApiService<JSONObject> implements IVerifyCodeService {

    @Autowired
    private RedisUtils redisUtil;

    @Override
    public BaseResponse<JSONObject> verificaWeixinCode(String phone, String weixinCode) {
        //1、验证参数是否为空
        if (StringUtils.isEmpty(phone)) {
            return setResultError("手机号码不能为空!");
        }
        //2、根据手机号码查询redis返回对应的注册码
        if (StringUtils.isEmpty(weixinCode)) {
            return setResultError("注册码不能为空!");
        }
        //3、redis中的注册码与传递参数的weixinCode进行比对
        String code = redisUtil.getString(Constants.WEIXINCODE_KEY + phone);
        if (StringUtils.isEmpty(code)) {
            return setResultError("注册码已经过期,请重新发送验证码");
        }
        if (!code.equals(weixinCode)) {
            return setResultError("注册码不正确");
        }
        //4、验证成功之后，移除验证码
        redisUtil.delKey(Constants.WEIXINCODE_KEY + phone);
        return setResultSuccess("注册码验证码正确");
    }

}
